import java.util.Arrays;

public class ArrayList {
    public int[] arr= new int[10];
    public int size=0;

    public void add(int val){
        if(size==arr.length){
            arr=grow();
        }
        arr[size]=val;
        size++;
    }

    public void add(int pos,int val) //在pos位置插入val1
    {
        if(!isLegal(pos)){
            return;
        }
        if(size==arr.length){
            arr=grow();
        }

        for(int i=size;i>=pos;i--){
            arr[i]=arr[i-1];
        }
        arr[pos]=val;
        size++;

    }

    public Boolean isLegal(int pos){
        if(pos<0||pos>arr.length){
            return false;
        }
        return true;
    }
    public int[] grow(){
        arr = Arrays.copyOf(arr,arr.length*2);
        return arr;
    }
    public void display(){
        for (int i = 0; i < size; i++) {
            System.out.print(arr[i]+" ");
        }
    }

    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }
        list.display();
      list.add(2,12);
        System.out.println();
      list.display();
      list.add(10,66);
        System.out.println();
        list.display();


    }
}
